//
//  LTFVideoView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/13.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFVideoView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFNavigationCollectionCell.h"
#import "LTFVideoContentCollectionViewCell.h"
#import "LTFVideoModel.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface LTFVideoView ()
<
 UICollectionViewDelegate,
 UICollectionViewDataSource
>

@property (nonatomic, retain) UICollectionView *navigationCollectionView;
@property (nonatomic, retain) NSMutableArray *collectionViewArray;

@property (nonatomic, retain) UICollectionView *contentCollectionView;
@property (nonatomic, retain) NSMutableArray *contentCollectionViewArray;
@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) NSString *str;

@property (nonatomic, retain) NSMutableArray *tableViewArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, assign)  NSInteger index;
@end


@implementation LTFVideoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        //[self createData];
    }
    return self;
}


- (void)createView {

    self.array = [NSArray arrayWithObjects:@"T1457068979049", @"T1457069041911", @"T1457069080899", @"T1457069205071", @"T1457069232830", @"T1457069261743", @"T1457069319264", @"T1457069346235", @"T1457069387259", @"T1457069475980", @"T1457069446903", @"T1457069421892", nil];

    
    //导航栏的数组
    self.collectionViewArray = [NSMutableArray arrayWithObjects:@"推荐", @"搞笑", @"美女", @"新闻现场", @"萌物", @"八卦", @"猎奇", @"体育", @"黑科技", @"涨姿势", @"二次元", @"军武", nil];
    
    //导航栏
    //底
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 5, 30);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    
    //导航栏
    self.navigationCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30) collectionViewLayout:layout];
    [self addSubview:self.navigationCollectionView];
    self.navigationCollectionView.delegate = self;
    self.navigationCollectionView.dataSource = self;
    [self.navigationCollectionView registerClass:[LTFNavigationCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFNavigationCollectionCell class])];
    
    
    
    //内容
    //底
    UICollectionViewFlowLayout *contentLayout = [[UICollectionViewFlowLayout alloc] init];
    contentLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 30);
    contentLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    contentLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    contentLayout.minimumLineSpacing = 0;
    
    //内容
    
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT - 30) collectionViewLayout:contentLayout];
    [self addSubview:self.contentCollectionView];
    self.contentCollectionView.delegate = self;
    self.contentCollectionView.dataSource = self;
    [self.contentCollectionView registerClass:[LTFVideoContentCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFVideoContentCollectionViewCell class])];
    self.contentCollectionView.pagingEnabled = YES;
    
}


//collectionView的第一个协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.contentCollectionView) {
        return 12;
    }
    else {
    return self.collectionViewArray.count;
    }
}
//collectionView的第二个协议
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.contentCollectionView) {
        LTFVideoContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFVideoContentCollectionViewCell class]) forIndexPath:indexPath];
//        cell.tableViewArray = self.tableViewArray;
        cell.str = self.array[indexPath.row];
        //cell.delegate = self;
        return cell;
    }
    else {
    LTFNavigationCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFNavigationCollectionCell class]) forIndexPath:indexPath];
        if (indexPath.row == self.index) {
            cell.backgroundColor = [UIColor redColor];
        }
        else {
            cell.backgroundColor = [UIColor blackColor];
        }
    cell.myLabel.text = self.collectionViewArray[indexPath.row];
    return cell;
    }
}

//导航栏点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.navigationCollectionView) {
        self.contentCollectionView.contentOffset = CGPointMake(indexPath.row * WIDTH, 0);
        
        
        
        LTFNavigationCollectionCell *newCell = (LTFNavigationCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
        newCell.backgroundColor = [UIColor redColor];
        LTFNavigationCollectionCell *oldCell = (LTFNavigationCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
        oldCell.backgroundColor = [UIColor blackColor];
        
        self.index = indexPath.row;
        [self.navigationCollectionView reloadData];
    }
}


- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.contentCollectionView) {
        CGFloat x = self.contentCollectionView.contentOffset.x;
        if ((NSInteger)(x / WIDTH) >= 7) {
            self.navigationCollectionView.contentOffset = CGPointMake(7 * WIDTH / 5, 0);
        }
        else {
            self.navigationCollectionView.contentOffset = CGPointMake(x / 5, 0);
        }
        self.index = (NSInteger)(x / WIDTH);
        [self.navigationCollectionView reloadData];
        
    }
}


- (void)dealloc {
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
